//
//  LoginTypeVM.swift
//  kxf
//
//  Created by Mac on 2024/3/4.
//

import Foundation
import UIKit

class LoginTypeVM: UIView {
    
    let selfHeight = kFitWidth(40)
    
    var loginType = "code"
    var typeChangeBlock:((String)->())?

    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var codeLoginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("验证码登录", for: .normal)
        btn.setTitleColor(UIColor.COLOR_TEXT_BLACK666, for: .normal)
        btn.setTitleColor(UIColor.THEME, for: .selected)
        btn.titleLabel?.font = .systemFont(ofSize: 14)
        btn.isSelected = true
        
        btn.addTarget(self, action: #selector(codeLoginAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var pswLoginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("账号登录", for: .normal)
        btn.setTitleColor(UIColor.COLOR_TEXT_BLACK666, for: .normal)
        btn.setTitleColor(UIColor.THEME, for: .selected)
        btn.titleLabel?.font = .systemFont(ofSize: 14)
        
        btn.addTarget(self, action: #selector(pswLoginAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var lineView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .THEME
        vi.layer.cornerRadius = kFitWidth(2)
        vi.clipsToBounds = true
        
        return vi
    }()
    
}

extension LoginTypeVM{
    @objc func codeLoginAction() {
        if loginType == "code"{
            return
        }
        loginType = "code"
        codeLoginBtn.isSelected = true
        pswLoginBtn.isSelected = false
        
        if self.typeChangeBlock != nil{
            self.typeChangeBlock!(self.loginType)
        }
        
        lineView.snp.remakeConstraints { make in
            make.centerX.lessThanOrEqualTo(codeLoginBtn)
            make.width.equalTo(kFitWidth(40))
            make.height.equalTo(kFitWidth(4))
            make.top.equalTo(codeLoginBtn.snp.bottom).offset(kFitWidth(3))
        }
    }
    @objc func pswLoginAction() {
        if loginType == "psw"{
            return
        }
        loginType = "psw"
        codeLoginBtn.isSelected = false
        pswLoginBtn.isSelected = true
        
        if self.typeChangeBlock != nil{
            self.typeChangeBlock!(self.loginType)
        }
        
        lineView.snp.remakeConstraints { make in
            make.centerX.lessThanOrEqualTo(pswLoginBtn)
            make.width.equalTo(kFitWidth(40))
            make.height.equalTo(kFitWidth(4))
            make.top.equalTo(pswLoginBtn.snp.bottom).offset(kFitWidth(3))
        }
    }
}
extension LoginTypeVM{
    func initUI() {
        addSubview(codeLoginBtn)
        addSubview(pswLoginBtn)
        addSubview(lineView)
        
        setConstrait()
    }
    func setConstrait() {
        codeLoginBtn.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(92))
            make.centerY.lessThanOrEqualToSuperview()
        }
        pswLoginBtn.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(220))
            make.centerY.lessThanOrEqualToSuperview()
        }
        lineView.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualTo(codeLoginBtn)
            make.width.equalTo(kFitWidth(40))
            make.height.equalTo(kFitWidth(4))
            make.top.equalTo(codeLoginBtn.snp.bottom).offset(kFitWidth(3))
        }
    }
}
